package com.qf.web.servlet;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 *
 */
@WebServlet("/createCode")
public class ValidateCodeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建验证码对象，  参数列表: 宽度，高度， 字符个数， 模糊线条个数
        ValidateCode vc = new ValidateCode(120, 40, 2, 20);;
        //生成的随机验证码
        String code = vc.getCode();

        //获取session
        HttpSession session = req.getSession();
        session.setAttribute("code", code);

        //获取 字节输出流对象
        ServletOutputStream outputStream = resp.getOutputStream();
        //把验证码图片输出到浏览器
        vc.write(outputStream);

    }
}
